using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;        // for "Directory", try/catch

namespace Tool
{
    public partial class Editor : Form
    {
        
        EventTimeEditor                 m_eventTimeEditor;
        SoundEditor                     m_soundEditor;
        public SoundLibrary             m_soundLibrary;         // sound file library
        public GlobalVariableManager    m_globalVarManager;

        About                   m_aboutDlg;             // dialog to display information about Best Tool Ever
        EventNameTextInput      m_textDlg;              // dialog to input an event name
        DeleteDialog            m_deleteDlg;            // dialog to confirm/deny delete item
        DeleteSoundFileDialog   m_deleteSoundDlg;       // dialong to confirm/deny delete sound
        GlobalVariableTextInput m_newGlobalVarDlg;      // dialog to add a new global variable
        DuplicateEventNameDlg   m_duplicateDlg;         // dialog to notify of duplicate object
        CantDelVar              m_delVarDlg;
        public CantFindWAV      m_cantFindWavDlg;

        public CantDelVar GetDelVarDlg()
        {
            return m_delVarDlg;
        }

        EventTree m_eventTree;
        public EventTree GetEventTree()
        {
            return m_eventTree;
        }

        const string FROM_SNDBANK_DROPDOWN_TXT = "From Sound Bank...";

        private bool m_insertETreeSnd = true;
        private bool m_losePanelFocus = true;

        ToolSystem  m_interface;
        public ToolSystem GetToolSystemInterface()
        {
            return m_interface;
        }

        HighLevelInterface m_highLevelInterface;

        public Editor()
        {
            // Required for Windows Form Designer support
            InitializeComponent();

            m_highLevelInterface    = new HighLevelInterface(this);
            m_interface             = new ToolSystem();

            unsafe
            {
                m_interface.Initialize(this.Handle.ToPointer());
            }

            // Create all necessary Dialogs
            m_aboutDlg        = new About();
            m_textDlg         = new EventNameTextInput(this);
            m_deleteDlg       = new DeleteDialog(this);
            m_deleteSoundDlg  = new DeleteSoundFileDialog(this);
            m_duplicateDlg    = new DuplicateEventNameDlg();
            m_delVarDlg       = new CantDelVar();
            m_newGlobalVarDlg = new GlobalVariableTextInput(this);
            m_cantFindWavDlg  = new CantFindWAV();

            m_eventTimeEditor   = new EventTimeEditor(this);
            m_globalVarManager  = new GlobalVariableManager(this);
            m_soundEditor       = new SoundEditor(this);
            m_soundLibrary      = new SoundLibrary(this, this.SoundBankGrid);
            m_eventTree         = new EventTree(this);

                // Set up the buffered graphics for the Sound Editor - THANK YOU, MEXICANS
                // Activating DoubleBuffer techniques. We must do it with reflection because Panel.SetStyle is a protected member.
            System.Reflection.MethodInfo m = typeof(Panel).GetMethod("SetStyle", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            Object[] parameters = new Object[2];
            parameters[0] = new ControlStyles();
            parameters[0] = ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint;
            parameters[1] = new bool();
            parameters[1] = true;
            m.Invoke(SoundEditorPanel, parameters);
            m = typeof(Panel).GetMethod("UpdateStyles", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            m.Invoke(SoundEditorPanel, null);
              // set up buffered graphics for the event time panel
            System.Reflection.MethodInfo m2 = typeof(Panel).GetMethod("SetStyle", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            Object[] parameters2 = new Object[2];
            parameters2[0] = new ControlStyles();
            parameters2[0] = ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint;
            parameters2[1] = new bool();
            parameters2[1] = true;
            m2.Invoke(EventTimePanel, parameters2);
            m2 = typeof(Panel).GetMethod("UpdateStyles", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            m2.Invoke(EventTimePanel, null);

            this.Show();
        }

        public void Update()
        {
            //m_interface.Update(16.67f);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openBTEFileDialog.ShowDialog() == DialogResult.OK)
            {
                m_highLevelInterface.LoadXMLFile(openBTEFileDialog.FileName);
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                m_highLevelInterface.SaveXMLFile(saveFileDialog.FileName);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        
        // Displays the "About" dialog when clicked in the help menu
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_aboutDlg.ShowDialog();
        }

        // Adds a new EventNode to the tree
        public void AddEventNode(TreeNode newNode)
        {
            EventTree.Nodes.Add(newNode);
        }

        // Delete given node from the tree
        public void DeleteEventNode(TreeNode delNode)
        {
            EventTree.Nodes.Remove(delNode);
        }

        // Get the currently selected node from the tree
        public TreeNode CurrentSelectedNode()
        {
            return EventTree.SelectedNode;
        }

        // When user clicks "Add Event", spawn input dialog
        private void AddEventButton_Click(object sender, EventArgs e)
        {
            // Create a default event name to display in the text field, then spawn the dialog
            m_textDlg.EventName.Text = "Event" + m_eventTree.GetEvents().Count;
            m_textDlg.ShowDialog();
        }

        private void EventTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
                // root node
            if (EventTree.SelectedNode.Parent == null)
            {
                SoundEditTypeComboBox.Enabled = false;
                SoundEditorTrackbar.Enabled = false;
                //UseGlobalVarCheckbox.Enabled = false;
                SoundGlobalVarComboBox.Enabled = false;
                PitchShiftNumericUpDown.Enabled = false;

                LoopCheckBox.Checked = m_eventTree.GetEvent(EventTree.SelectedNode.Text).GetLoop();
                
				m_eventTimeEditor.SelectSound(-1);
            }
            else
            {
                PitchShiftNumericUpDown.Enabled = true;
                PitchShiftNumericUpDown.Value = m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).GetPitchShift();

                SoundEditTypeComboBox.Enabled = true;

                if (SoundEditTypeComboBox.Text == "")
                {
                    SoundEditTypeComboBox.SelectedIndex = 0;
                    SoundEditTypeComboBox.Text = SoundEditTypeComboBox.SelectedText;
                }

                SoundEditorTrackbar.Enabled = false;
                //UseGlobalVarCheckbox.Enabled = true;
                //UseGlobalVarCheckbox.Checked = false;

                LoopCheckBox.Checked = m_eventTree.GetEvent(EventTree.SelectedNode.Parent.Text).GetLoop();

                EventSound currSound = m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index);

                switch (SoundEditTypeComboBox.SelectedIndex)
                {
                    case (int)(EDIT_TYPE.EDIT_PAN): // Pan
                        {
                            if (m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).IsUsingVar(EDIT_TYPE.EDIT_PAN))//.m_isUsingGlobalVar4Pan)
                            {
                                SoundGlobalVarComboBox.Enabled = true;
                                //UseGlobalVarCheckbox.Checked = true;
                                    // Cant use the trackbar if it is global
                                SoundEditorTrackbar.Enabled = true;
                                SoundEditorTrackbar.Value = (int)(m_globalVarManager.GetValue(currSound.GetVarName(EDIT_TYPE.EDIT_PAN)/*.m_globalVarName4Pan*/) * 1000.0f);
                                SoundGlobalVarComboBox.Text = m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).GetVarName(EDIT_TYPE.EDIT_PAN);//.m_globalVarName4Pan;
                            }
                            break;
                        }
                    case (int)(EDIT_TYPE.EDIT_VOLUME): // Volume
                        {
                            if (m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).IsUsingVar(EDIT_TYPE.EDIT_VOLUME))//.m_isUsingGlobalVar4Volume)
                            {
                                SoundGlobalVarComboBox.Enabled = true;
                                //UseGlobalVarCheckbox.Checked = true;
                                // Cant use the trackbar if it is global
                                SoundEditorTrackbar.Enabled = true;
                                SoundEditorTrackbar.Value = (int)(m_globalVarManager.GetValue(currSound.GetVarName(EDIT_TYPE.EDIT_VOLUME)/*.m_globalVarName4Volume*/) * 1000.0f);
                                SoundGlobalVarComboBox.Text = m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).GetVarName(EDIT_TYPE.EDIT_VOLUME);//.m_globalVarName4Volume;
                            }
                            break;
                        }
                    case (int)(EDIT_TYPE.EDIT_DISTORTION):
                        {
                            if (m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).IsUsingVar(EDIT_TYPE.EDIT_DISTORTION))//.m_isUsingGlobalVar4Pan)
                            {
                                SoundGlobalVarComboBox.Enabled = true;
                                // Cant use the trackbar if it is global
                                SoundEditorTrackbar.Enabled = true;
                                SoundEditorTrackbar.Value = (int)(m_globalVarManager.GetValue(currSound.GetVarName(EDIT_TYPE.EDIT_DISTORTION)/*.m_globalVarName4Pan*/) * 1000.0f);
                                SoundGlobalVarComboBox.Text = m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).GetVarName(EDIT_TYPE.EDIT_DISTORTION);//.m_globalVarName4Pan;
                            }
                            break;
                        }
                    case (int)(EDIT_TYPE.EDIT_REVERB):
                        {
                            if (m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).IsUsingVar(EDIT_TYPE.EDIT_REVERB))//.m_isUsingGlobalVar4Pan)
                            {
                                SoundGlobalVarComboBox.Enabled = true;
                                // Cant use the trackbar if it is global
                                SoundEditorTrackbar.Enabled = true;
                                SoundEditorTrackbar.Value = (int)(m_globalVarManager.GetValue(currSound.GetVarName(EDIT_TYPE.EDIT_REVERB)/*.m_globalVarName4Pan*/) * 1000.0f);
                                SoundGlobalVarComboBox.Text = m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).GetVarName(EDIT_TYPE.EDIT_REVERB);//.m_globalVarName4Pan;
                            }
                            break;
                        }
                }

                SoundGlobalVarComboBox.Items.Clear();
                SoundGlobalVarComboBox.Enabled = true;

                EDIT_TYPE editType = ((EDIT_TYPE)SoundEditTypeComboBox.SelectedIndex);

                ArrayList varList = m_globalVarManager.GetGlobalVariables();
                for (int i = 0; i < varList.Count; ++i)
                {
                    SoundGlobalVarComboBox.Items.Add(((GlobalVariable)varList[i]).m_name);
                }
                int varIndx = SoundGlobalVarComboBox.FindStringExact(currSound.GetVarName(editType));
                SoundGlobalVarComboBox.SelectedIndex = varIndx;


				m_eventTimeEditor.SelectSound(EventTree.SelectedNode.Index);
            }

            SoundEditorPanel.Invalidate();
            EventTimePanel.Invalidate();
        }

        // Ensures the node you right/left-click on is the current node
        private void EventTree_MouseClick(object sender, MouseEventArgs e)
        {
            TreeNode tempNode = EventTree.GetNodeAt(e.X, e.Y);

            // set our current node to where the user clicked
            EventTree.SelectedNode = tempNode;

        }
            // OMG Drap and Drop
        private void SoundBankGrid_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.All;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void SoundBankGrid_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            int j;
            for (j = 0; j < s.Length; ++j)
            {
                if (s[j].ToUpper().EndsWith(".WAV"))
                {
                    bool add = true;
                    //for (int i = 0; i < s[j].Length; ++i)
                    //{
                        // check that the file isn't already in the library
                        for (int index = 0; index < m_soundLibrary.m_Size; ++index)
                        {
                            // if the file is already in the soundbank...
                            if (((SoundFile)m_soundLibrary.m_FileList[index]).GetFilePath() == s[j])
                            {
                                string parsedFilename = ((SoundFile)m_soundLibrary.m_FileList[index]).ParseFilename();
                                m_duplicateDlg.label1.Text = "A sound with the name \"" + parsedFilename + "\" already exists in the sound bank.";

                                m_duplicateDlg.button1.Location = new Point(m_duplicateDlg.Width / 2 - m_duplicateDlg.button1.Width / 2, m_duplicateDlg.button1.Location.Y);
                                m_duplicateDlg.ShowDialog();
                                add = false;
                                break;
                            }
                        }

                        if (!add)
                            continue;

                        // file doesn't already exist, add it to the library
                        SoundBankGrid.Invalidate();
                        SoundFile newSound = new SoundFile(s[j],this);
                        m_soundLibrary.AddFile(newSound);
                    //}
                    //m_soundLibrary.AddFile(new SoundFile(s[j]));
                }
            }
        }

        // Detects when the mouse was clicked in the Sound Bank
        private void SoundBankGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            // for any mouse button clicked...
            switch (e.Button)
            {
                // if the right mouse button was clicked...
                case (MouseButtons.Right):
                    {
                        // if there are currently rows selected...
                        if (SoundBankGrid.SelectedRows.Count != 0)
                        {
                            // deselect the rows
                            SoundBankGrid.Rows[SoundBankGrid.SelectedRows[0].Index].Selected = false;
                        }

                        // if the index at which the user clicked is valid...
                        if (e.RowIndex >= 0)
                        {
                            // set that row to be selected
                            SoundBankGrid.Rows[e.RowIndex].Selected = true;                            
                        }
       
                        break;
                    }
            }
        }

        private void SoundEditor_Paint(object sender, PaintEventArgs e)
        {
            m_soundEditor.Draw(e.Graphics);
        }

        private void SoundEditor_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            switch (e.Button)
            {
                case (MouseButtons.Left):
                    {
                        m_soundEditor.MouseDown(e.X, e.Y);
                        SoundEditorPanel.Invalidate();
						EventTimePanel.Invalidate();
                        break;
                    }
                case (MouseButtons.Right):
                    {
                        break;
                    }
                default:
                    {
                        m_soundEditor.MouseUp();
                        break;
                    }
            }
        }

        private void SoundEditor_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            switch (e.Button)
            {
                case (MouseButtons.Right):
                    {
                        m_soundEditor.RightClick(e.X, e.Y);

                        break;
                    }
            }
        }

        private void SoundEditTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            SoundGlobalVarComboBox.Enabled = false;
            //UseGlobalVarCheckbox.Checked = false;
            //SoundEditorTrackbar.Enabled = false;

            switch (SoundEditTypeComboBox.SelectedIndex)
            {
                case (int)(EDIT_TYPE.EDIT_PAN): // Pan
                    {
                        EventSound currSound = m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index);
                        if (currSound.IsUsingVar(EDIT_TYPE.EDIT_PAN))//.m_isUsingGlobalVar4Pan)
                        {
                            SoundGlobalVarComboBox.Enabled = true;
                            SoundGlobalVarComboBox.Text = currSound.GetVarName(EDIT_TYPE.EDIT_PAN);//m_globalVarName4Pan;
                            //UseGlobalVarCheckbox.Checked = true;
                            // Cant use the trackbar if it is global
                            SoundEditorTrackbar.Enabled = true;

                            SoundEditorTrackbar.Value = (int)(m_globalVarManager.GetValue(currSound.GetVarName(EDIT_TYPE.EDIT_PAN)/*.m_globalVarName4Pan*/) * 1000.0f);
                        }
                        break;
                    }
                case (int)(EDIT_TYPE.EDIT_VOLUME): // Volume
                    {
                        EventSound currSound = m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index);
                        if (currSound.IsUsingVar(EDIT_TYPE.EDIT_VOLUME))
                        {
                            SoundGlobalVarComboBox.Enabled = true;
                            SoundGlobalVarComboBox.Text = currSound.GetVarName(EDIT_TYPE.EDIT_VOLUME);
                            //UseGlobalVarCheckbox.Checked = true;
                            // Cant use the trackbar if it is global
                            SoundEditorTrackbar.Enabled = true;

                            SoundEditorTrackbar.Value = (int)(m_globalVarManager.GetValue(currSound.GetVarName(EDIT_TYPE.EDIT_VOLUME)) * 1000.0f);
                        }
                        break;
                    }
                case (int)(EDIT_TYPE.EDIT_DISTORTION): // Distortion
                    {
                        EventSound currSound = m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index);
                        if (currSound.IsUsingVar(EDIT_TYPE.EDIT_DISTORTION))
                        {
                            SoundGlobalVarComboBox.Enabled = true;
                            SoundGlobalVarComboBox.Text = currSound.GetVarName(EDIT_TYPE.EDIT_DISTORTION);
                            // Cant use the trackbar if it is global
                            SoundEditorTrackbar.Enabled = true;

                            SoundEditorTrackbar.Value = (int)(m_globalVarManager.GetValue(currSound.GetVarName(EDIT_TYPE.EDIT_DISTORTION)) * 1000.0f);
                        }
                        break;
                    }
                case (int)(EDIT_TYPE.EDIT_REVERB): // Reverb
                    {
                        EventSound currSound = m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index);
                        if (currSound.IsUsingVar(EDIT_TYPE.EDIT_REVERB))
                        {
                            SoundGlobalVarComboBox.Enabled = true;
                            SoundGlobalVarComboBox.Text = currSound.GetVarName(EDIT_TYPE.EDIT_REVERB);
                            // Cant use the trackbar if it is global
                            SoundEditorTrackbar.Enabled = true;

                            SoundEditorTrackbar.Value = (int)(m_globalVarManager.GetValue(currSound.GetVarName(EDIT_TYPE.EDIT_REVERB)) * 1000.0f);
                        }
                        break;
                    }

                default:
                    SoundEditorTrackbar.Value = 0;
                    break;
            }


            SoundEditorPanel.Invalidate();
			EventTimePanel.Invalidate();
        }

        private void addPointToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_soundEditor.AddPoint();
            SoundEditorPanel.Invalidate();
        }

        private void addSoundToolStripMenuItem_DDOpened(object sender, EventArgs e)
        {
            if( !AddSoundDropDown.Items.Contains( FROM_SNDBANK_DROPDOWN_TXT ) )
            {
                AddSoundDropDown.Items.Insert(0, FROM_SNDBANK_DROPDOWN_TXT);
            }

            AddSoundDropDown.SelectedItem = FROM_SNDBANK_DROPDOWN_TXT;
        }

        private void AddSoundDropDown_DDClosed(object sender, EventArgs e)
        {
            AddSoundDropDown_SelectedIndexChanged(sender, e);

            //if (AddSoundDropDown.SelectedItem == null)
                addSoundToolStripMenuItem_DDOpened(sender, e);
        }

        private void AddSoundDropDown_DropDown(object sender, EventArgs e)
        {
            AddSoundDropDown.Items.Remove(FROM_SNDBANK_DROPDOWN_TXT);
            AddSoundDropDown.SelectedIndex = -1;
        }

        // Add the sound selected from the dropdown list
        private void AddSoundDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!m_insertETreeSnd)
            {
                m_insertETreeSnd = true;
                return;
            }

            if (AddSoundDropDown.SelectedItem == null)
                return;
            if (AddSoundDropDown.SelectedItem.ToString() == FROM_SNDBANK_DROPDOWN_TXT)
                return;

            // Create a new node and add it to the tree
            TreeNode temp           = new TreeNode();
            temp.Text               = AddSoundDropDown.SelectedItem.ToString();
            temp.ContextMenuStrip   = SoundsRightClickMenu;

            EventTree.SelectedNode.Nodes.Add(temp);
            EventTree.SelectedNode.Expand();

            m_eventTree.AddSound(EventTree.SelectedNode.Text, temp.Text);

            //addSoundToolStripMenuItem_DDOpened(sender, e);
        }

        private void AddSoundDropDown_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;

            if (AddSoundDropDown.Items.Contains(FROM_SNDBANK_DROPDOWN_TXT))
            {
                AddSoundDropDown.Items.Remove(FROM_SNDBANK_DROPDOWN_TXT);
            }

            switch (e.KeyCode)
            {
                case Keys.Down:
                    m_insertETreeSnd = false;
                    AddSoundDropDown.SelectedIndex = (AddSoundDropDown.SelectedIndex + 1) %
                        AddSoundDropDown.Items.Count; break;

                case Keys.Up:
                    m_insertETreeSnd = false;

                    if (AddSoundDropDown.SelectedIndex == -1)
                    {
                        AddSoundDropDown.SelectedIndex = AddSoundDropDown.Items.Count-1;
                        break;
                    }

                    int newIndx = (AddSoundDropDown.SelectedIndex - 1) %
                        AddSoundDropDown.Items.Count;
                    newIndx = newIndx < 0 ? AddSoundDropDown.Items.Count + newIndx : newIndx;
                    AddSoundDropDown.SelectedIndex = newIndx; break;

                case Keys.Enter:
                    AddSoundDropDown_SelectedIndexChanged(sender, e); break;
            }
        }

        // Bring up a dialog to confirm delete-event
        private void DeleteCurrentEvent_Click(object sender, EventArgs e)
        {
            m_deleteDlg.ShowDialog();
        }

        // Delete the current point from the graph
        private void RemovePoint_Click(object sender, EventArgs e)
        {
            m_soundEditor.DeletePoint();
        }

        // Delete a sound from the the event tree
        private void DeleteCurrentSound_Click(object sender, EventArgs e)
        {
            SoundEditorPanel.Invalidate();
            m_deleteDlg.ShowDialog();
        }

        private void SoundEditorTrackbar_Scroll(object sender, EventArgs e)
        {
            EventSound sound = m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index);

            string globalVarName = "";

            switch (SoundEditTypeComboBox.SelectedIndex)
            {
                case (int)(EDIT_TYPE.EDIT_PAN): // Pan
                    {
                        if (sound.IsUsingVar(EDIT_TYPE.EDIT_PAN))//.m_isUsingGlobalVar4Pan)
                        {
                            globalVarName = sound.GetVarName(EDIT_TYPE.EDIT_PAN);//.m_globalVarName4Pan;
                        }
                        break;
                    }
                case (int)(EDIT_TYPE.EDIT_VOLUME): // Volume
                    {
                        if (sound.IsUsingVar(EDIT_TYPE.EDIT_VOLUME))
                        {
                            globalVarName = sound.GetVarName(EDIT_TYPE.EDIT_VOLUME);
                        }
                        break;
                    }
                case (int)(EDIT_TYPE.EDIT_DISTORTION): // Distortion
                    {
                        if (sound.IsUsingVar(EDIT_TYPE.EDIT_DISTORTION))
                        {
                            globalVarName = sound.GetVarName(EDIT_TYPE.EDIT_DISTORTION);
                        }
                        break;
                    }
                case (int)(EDIT_TYPE.EDIT_REVERB): // Reverb
                    {
                        if (sound.IsUsingVar(EDIT_TYPE.EDIT_REVERB))
                        {
                            globalVarName = sound.GetVarName(EDIT_TYPE.EDIT_REVERB);
                        }
                        break;
                    }
            }

            m_globalVarManager.SetValue(globalVarName, (float)SoundEditorTrackbar.Value / 1000.0f);
            SoundEditorPanel.Invalidate();
        }

        // Adds a sound to the Sound Bank
        private void AddSoundButton_Click(object sender, EventArgs e)
        {
            // Set the Open File text box to be blank
            openSoundFileDialog.FileName = " ";

            // if the user has selected a file, and clicked "OK"...
            if (this.openSoundFileDialog.ShowDialog() == DialogResult.OK)
            {
                // get the file's filename
                string[] filenames = openSoundFileDialog.FileNames;
                bool add = true;

                // 
                for (int i = 0; i < filenames.Length; ++i)
                {
                    // check that the file isn't already in the library
                    for (int index = 0; index < m_soundLibrary.m_Size; ++index)
                    {
                        // if the file is already in the soundbank...
                        if( ((SoundFile)m_soundLibrary.m_FileList[index]).GetFilePath() == filenames[i] )
                        {
                            // Parse the file path, and notify user that the file already exists in the bank
                            string parsedFilename = ((SoundFile)m_soundLibrary.m_FileList[index]).ParseFilename();
                            m_duplicateDlg.label1.Text = "A sound with the name \"" + parsedFilename + "\" already exists in the sound bank.";

                            // Formatting, center the "OK" button based off of the dialog box size, then display dialog
                            m_duplicateDlg.button1.Location = new Point(m_duplicateDlg.Width / 2 - m_duplicateDlg.button1.Width / 2, m_duplicateDlg.button1.Location.Y);
                            m_duplicateDlg.ShowDialog();

                            // Set the commmand not to add a new file
                            add = false;

                            break;
                        }
                    }

                    // if the file is already in the bank, this value will be false, don't add the sound to the bank
                    if (!add)
                        continue;

                    // file doesn't already exist, add it to the bank/library
                    SoundBankGrid.Invalidate();
                    SoundFile newSound = new SoundFile(filenames[i],this);
                    m_soundLibrary.AddFile(newSound);
                }
            }
        }

        // Delete a sound from the soundbank
        private void DeleteSound_Click(object sender, EventArgs e)
        {
            SoundBankGrid.Invalidate();
            m_deleteSoundDlg.ShowDialog();
        }

        private void AddGlobalVariableButton_Click(object sender, EventArgs e)
        {
            int endNum = m_globalVarManager.GetGlobalVariables().Count;
            m_newGlobalVarDlg.GlobalVariableName.Text = "Variable" + endNum;

            ArrayList globalVars = m_globalVarManager.GetGlobalVariables();
            for (int i = 0; i < globalVars.Count; ++i)
            {
                if (((GlobalVariable)globalVars[i]).m_name == m_newGlobalVarDlg.GlobalVariableName.Text)
                {
                    m_newGlobalVarDlg.GlobalVariableName.Text = "Variable" + ++endNum;
                    i = -1;
                }
            }

            m_newGlobalVarDlg.ShowDialog();
        }

        private void SoundGlobalVarComboBox_DropDown(object sender, EventArgs e)
        {
            ArrayList globalVars = m_globalVarManager.GetGlobalVariables();

            SoundGlobalVarComboBox.Items.Clear();

            for (int i = 0; i < globalVars.Count; ++i)
            {
                SoundGlobalVarComboBox.Items.Add(((GlobalVariable)globalVars[i]).m_name);
            }
        }

        private void SoundGlobalVarComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (EventTree.SelectedNode == null)
                return;

            //SoundEditorTrackbar.Enabled = false;
            switch (SoundEditTypeComboBox.SelectedIndex)
            {
                case (int)(EDIT_TYPE.EDIT_PAN): // Pan
                    {
                        //m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).m_isUsingGlobalVar4Pan = true;
                        m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).SetVarName(EDIT_TYPE.EDIT_PAN,SoundGlobalVarComboBox.Text);//.m_globalVarName4Pan = SoundGlobalVarComboBox.Text;
                        break;
                    }
                case (int)(EDIT_TYPE.EDIT_VOLUME): // Volume
                    {
                        //m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).m_isUsingGlobalVar4Volume = true;
                        m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).SetVarName(EDIT_TYPE.EDIT_VOLUME, SoundGlobalVarComboBox.Text);//.m_globalVarName4Volume = SoundGlobalVarComboBox.Text;
                        break;
                    }
                case (int)(EDIT_TYPE.EDIT_DISTORTION): // Distortion
                    {
                        m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).SetVarName(EDIT_TYPE.EDIT_DISTORTION, SoundGlobalVarComboBox.Text);//.m_globalVarName4Volume = SoundGlobalVarComboBox.Text;
                        break;
                    }
                case (int)(EDIT_TYPE.EDIT_REVERB): // Reverb
                    {
                        m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).SetVarName(EDIT_TYPE.EDIT_REVERB, SoundGlobalVarComboBox.Text);//.m_globalVarName4Volume = SoundGlobalVarComboBox.Text;
                        break;
                    }
            }

            for (int i = 0; i < GlobalVariableDataGrid.Rows.Count; ++i)
            {
                if (GlobalVariableDataGrid.Rows[i].Cells[0].Value.ToString() == SoundGlobalVarComboBox.Text)
                {
                    GlobalVariableDataGrid.Rows[i].Selected = true;

                    GlobalVariableDataGrid.FirstDisplayedScrollingRowIndex =
                    GlobalVariableDataGrid.SelectedRows[0].Index;
                    break;
                }
            }

            float val = m_globalVarManager.GetValue(SoundGlobalVarComboBox.Text);
            if( val >= 0.0f )
            {
                SoundEditorTrackbar.Value = (int)(val * 1000);
                SoundEditorPanel.Invalidate();
            }
        }

        private void SoundGlobalVarComboBox_DropDownClosed(object sender, EventArgs e)
        {
            if (SoundGlobalVarComboBox.SelectedIndex < 0 &&
                EventTree.SelectedNode != null )
            {
                EventSound currSound = m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index);

                SoundGlobalVarComboBox.SelectedIndex = SoundGlobalVarComboBox.FindStringExact(
                    currSound.GetVarName(((EDIT_TYPE)SoundEditTypeComboBox.SelectedIndex)));
            }
        }

        //private void UseGlobalVarCheckbox_Click(object sender, EventArgs e)
        //{
        //        // I assume we must have a sound selected or this wouldn't even be enabled
        //    if (UseGlobalVarCheckbox.Checked)
        //    {
        //            // Add items to the drop down, and then select the first one
        //        ArrayList globalVars = m_globalVarManager.GetGlobalVariables();

        //        SoundGlobalVarComboBox.Items.Clear();

        //        for (int i = 0; i < globalVars.Count; ++i)
        //        {
        //            SoundGlobalVarComboBox.Items.Add(((GlobalVariable)globalVars[i]).m_name);
        //        }

        //        SoundGlobalVarComboBox.Enabled = true;

        //        if (m_globalVarManager.GetGlobalVariables().Count == 0)
        //        {
        //            SoundGlobalVarComboBox.Enabled = false;
        //            //UseGlobalVarCheckbox.Enabled = false;
        //            UseGlobalVarCheckbox.Checked = false;
        //            return;
        //        }

        //        SoundGlobalVarComboBox.SelectedIndex = 0;

        //        SoundEditorTrackbar.Enabled = true;

        //        GlobalVariable defaultVar = new GlobalVariable("poop");
        //        if( SoundGlobalVarComboBox.Text == "" )
        //        {
        //            defaultVar = ((GlobalVariable)m_globalVarManager.GetGlobalVariables()[0]);
        //        }
        //        else
        //        {
        //            defaultVar = m_globalVarManager.GetGlobalVariable(SoundGlobalVarComboBox.Text);
        //        }

        //        switch (SoundEditTypeComboBox.SelectedIndex)
        //        {
        //            case 0: // Pan
        //                {
        //                    m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).SetIsUsingVar(EDIT_TYPE.EDIT_PAN, true);//.m_isUsingGlobalVar4Pan = true;
        //                    m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).SetVarName(EDIT_TYPE.EDIT_PAN, defaultVar.m_name);//.m_globalVarName4Pan = defaultVar.m_name;
        //                    break;
        //                }
        //            case 1: // Volume
        //                {
        //                    m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).SetIsUsingVar(EDIT_TYPE.EDIT_VOLUME, true);//.m_isUsingGlobalVar4Volume = true;
        //                    m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).SetVarName(EDIT_TYPE.EDIT_VOLUME, defaultVar.m_name);//.m_globalVarName4Volume = defaultVar.m_name;
        //                    break;
        //                }
        //        }
        //    }
        //    else
        //    {
        //        SoundGlobalVarComboBox.Text = "";
        //        SoundEditorTrackbar.Enabled = false;
        //        SoundGlobalVarComboBox.Enabled = false;

        //        switch (SoundEditTypeComboBox.SelectedIndex)
        //        {
        //            case 0: // Pan
        //                {
        //                    m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).SetIsUsingVar(EDIT_TYPE.EDIT_PAN, false);//.m_isUsingGlobalVar4Pan = false;
        //                    break;
        //                }
        //            case 1: // Volume
        //                {
        //                    m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).SetIsUsingVar(EDIT_TYPE.EDIT_VOLUME, false);//.m_isUsingGlobalVar4Volume = false;
        //                    break;
        //                }
        //        }
        //    }
        //}

            // Allows the rows in the global variable grid to be selected by right clicking
        private void GlobalVariableDataGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            switch (e.Button)
            {
                case (MouseButtons.Right):
                    {
                        if (e.RowIndex >= 0)
                        {
                            if (GlobalVariableDataGrid.SelectedRows.Count == 0)
                                GlobalVariableDataGrid.Rows[e.RowIndex].Selected = true;

                            GlobalVariableDataGrid.Rows[GlobalVariableDataGrid.SelectedRows[0].Index].Selected = false;
                            GlobalVariableDataGrid.Rows[e.RowIndex].Selected = true;
                            
                        }
                        break;
                    }

                case MouseButtons.Left:
                    {
                        if (e.RowIndex >= 0 && SoundGlobalVarComboBox.Enabled)
                        {
                            EventSound currSound = m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text,
                                EventTree.SelectedNode.Index);
                            currSound.SetVarName(((EDIT_TYPE)SoundEditTypeComboBox.SelectedIndex),
                                GlobalVariableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString());

                            SoundGlobalVarComboBox.Items.Clear();
                            ArrayList globalVars = m_globalVarManager.GetGlobalVariables();
                            for (int i = 0; i < globalVars.Count; ++i)
                            {
                                SoundGlobalVarComboBox.Items.Add(((GlobalVariable)globalVars[i]).m_name);
                            }

                            int comboIndx = SoundGlobalVarComboBox.FindStringExact(
                                GlobalVariableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString());
                            if (comboIndx >= 0)
                                SoundGlobalVarComboBox.SelectedIndex = comboIndx;

                            float val = m_globalVarManager.GetValue(SoundGlobalVarComboBox.Text);
                            if (val >= 0.0f)
                            {
                                SoundEditorTrackbar.Value = (int)(val * 1000);
                                SoundEditorPanel.Invalidate();
                            }
                        }

                        break;
                    }
            }
        }

        //private void GlobalVariableDataGrid_MouseDown(object sender, MouseEventArgs e)
        private void GlobalVariableDataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //DataGridViewSelectedRowCollection rows = GlobalVariableDataGrid.SelectedRows;

            //if( rows.Count == 0 )
            //{
            //    return;
            //}

            //    // There should only be 1 selected at a time evar

            //if (rows[0].Cells[1].Value != null) // something has been selected
            //{
            //    m_globalVarManager.GetGlobalVariable(rows[0].Cells[0].Value.ToString()).m_value = (float)(Convert.ToDouble(rows[0].Cells[1].Value.ToString()));
            //}

                // Try looping through all rows always...
            //DataGridViewRowCollection rows = GlobalVariableDataGrid.Rows;

            //for (int i = 0; i < rows.Count; ++i)
            //{
            //    if (rows[i].Cells[1].Value != null)
            //    {
            //        m_globalVarManager.GetGlobalVariable(rows[i].Cells[0].Value.ToString()).m_value = (float)(Convert.ToDouble(rows[i].Cells[1].Value.ToString()));
            //    }
            //}

            //SoundEditorPanel.Invalidate();
        }
        
        private void EventTimePanel_Paint(object sender, PaintEventArgs e)
        {
            m_eventTimeEditor.Draw(e.Graphics);
        }

        private void EventTimePanel_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            EventTimePanel.Focus();

            switch (e.Button)
            {
                case (MouseButtons.Left):
                    m_eventTimeEditor.OnLMouseDown(e.X, e.Y);
                    EventTimePanel.Invalidate(); break;
                case (MouseButtons.Right):
                    m_eventTimeEditor.OnRMouseDown(e.X, e.Y); break;
            }
        }

        private void EventTimePanel_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            switch (e.Button)
            {
                case (MouseButtons.Left):
                    m_eventTimeEditor.OnLMouseUp();
                    EventTimePanel.Invalidate(); break;
            }
        }

        private void EventTimePanel_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            switch (e.Button)
            {
                case (MouseButtons.Left):
                    m_eventTimeEditor.OnMouseMove(e.X,e.Y);
                    EventTimePanel.Invalidate(); break;
            }
        }

        private void EventTimePanel_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            int delta = -e.Delta / 120 * EventTimeScroll.SmallChange;
            int maxVal = EventTimeScroll.Maximum - EventTimeScroll.LargeChange + 1;

            if (EventTimeScroll.Value + delta < 0)
                EventTimeScroll.Value = 0;
            else if (EventTimeScroll.Value + delta > maxVal)
                EventTimeScroll.Value = maxVal;
            else
                EventTimeScroll.Value += delta;

            EventTimePanel.Invalidate();
        }

        private void EventTimeScroll_Scroll(object sender, ScrollEventArgs e)
        {
            EventTimePanel.Invalidate();
        }

        private void EventTimePanel_MouseDblClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //switch (e.Button)
            //{
                //case (MouseButtons.Left):
                    //m_eventTimeEditor.OnMouseDblClick(e.X, e.Y);
                    //EventTimePanel.Invalidate(); break;
            //}
        }

        private void EventTimePanel_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Left:
                    m_losePanelFocus = false;
                    m_eventTimeEditor.ShiftSelectedSnd(-10);
                    EventTimePanel.Invalidate(); break;
                    
                case Keys.Right:
                    m_losePanelFocus = false;
                    m_eventTimeEditor.ShiftSelectedSnd(10);
                    EventTimePanel.Invalidate(); break;

                case Keys.Down:
                    m_losePanelFocus = false;
                    m_eventTimeEditor.SelectNextSound(true);
                    SoundEditorPanel.Invalidate();
                    EventTimePanel.Invalidate(); break;

                case Keys.Up:
                    m_losePanelFocus = false;
                    m_eventTimeEditor.SelectNextSound(false);
                    EventTimePanel.Invalidate(); break;
            }
        }

        private void EventTimePanel_LostFocus(object sender, EventArgs e)
        {
            if (!m_losePanelFocus)
            {
                m_losePanelFocus = true;
                EventTimePanel.Focus();
            }

            EventTimePanel.Invalidate();
        }

        private void GlobalVarDeleteRightClick_Click(object sender, EventArgs e)
        {
            m_globalVarManager.RemoveGlobalVariable(GlobalVariableDataGrid.SelectedRows[0].Cells[0].Value.ToString());
        }

        private void PitchShiftNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            m_eventTree.GetEventSound(EventTree.SelectedNode.Parent.Text, EventTree.SelectedNode.Index).SetPitchShift((int)(PitchShiftNumericUpDown.Value));
        }

        // Add a file to the Sound Bank via the event tree
        private void fromFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Set the text box in the Open File dialog to be empty
            openSoundFileDialog.FileName = "";

            // if the user has selected file(s) and clicked "OK"...
            if (this.openSoundFileDialog.ShowDialog() == DialogResult.OK)
            {
                // Get the file path(s)
                string[] filenames = openSoundFileDialog.FileNames;

                // for each file path...
                for (int i = 0; i < filenames.Length; ++i)
                {
                    // We want to add the file to the bank/library
                    bool add = true;
                    
                    // check that the file isn't already in the library...
                    for (int index = 0; index < m_soundLibrary.m_Size; ++index)
                    {
                        // if the file is already in the soundbank...
                        if (((SoundFile)m_soundLibrary.m_FileList[index]).GetFilePath() == filenames[i])
                        {
                            // Parse the file name, and notify user of duplicate
                            string parsedFilename = ((SoundFile)m_soundLibrary.m_FileList[index]).ParseFilename();
                            m_duplicateDlg.label1.Text = "A sound with the name \"" + parsedFilename + "\" already exists in the sound bank.";

                            // Formatting, center the "OK" button based off of the dialog box
                            m_duplicateDlg.button1.Location = new Point(m_duplicateDlg.Width / 2 - m_duplicateDlg.button1.Width/2, m_duplicateDlg.button1.Location.Y);
                            m_duplicateDlg.ShowDialog();
                            
                            // We don't want to add the sound
                            add = false;

                            break;
                        }
                    }

                    // "add" will have been set to false if the sound isn't to be added
                    if (!add)
                        continue;

                    // file doesn't already exist, add it to the library
                    SoundBankGrid.Invalidate();
                    SoundFile newSound = new SoundFile(filenames[i], this);
                    m_soundLibrary.AddFile(newSound);

                    // create a new treee node and add the sound to the current event
                    TreeNode temp = new TreeNode();
                    temp.Text = AddSoundDropDown.Items[AddSoundDropDown.Items.Count - 1].ToString();
                    temp.ContextMenuStrip = SoundsRightClickMenu;
                    EventTree.SelectedNode.Nodes.Add(temp);
                    EventTree.SelectedNode.Expand();

                    m_eventTree.AddSound(EventTree.SelectedNode.Text, temp.Text);
                }
            
            }
        }

        private void deleteFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SoundBankGrid.Invalidate();
            m_deleteSoundDlg.ShowDialog();
        }

        private void SoundBankGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 6)
            {
                bool temp = !(bool)(SoundBankGrid.Rows[SoundBankGrid.SelectedRows[0].Index].Cells[6].Value);
                SoundBankGrid.Rows[SoundBankGrid.SelectedRows[0].Index].Cells[6].Value = temp;

                m_soundLibrary.GetFileByFilename(SoundBankGrid.Rows[SoundBankGrid.SelectedRows[0].Index].Cells[0].Value.ToString()).SetStreaming(temp, SoundBankGrid.SelectedRows[0].Index);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            string eventName = "poop";

            if (EventTree.SelectedNode.Parent == null)
            {
                eventName = EventTree.SelectedNode.Text;
            }
            else
            {
                eventName = EventTree.SelectedNode.Parent.Text;
            }

            Event evnt = m_eventTree.GetEvent(eventName);

            evnt.SetLoop(LoopCheckBox.Checked);
        }

        private void renameEventToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_textDlg.EventName.Text = EventTree.SelectedNode.Text;
            m_textDlg.m_renamingEvnt = true;
            m_textDlg.ShowDialog();
        }

        private void NewMenuItem_Click(object sender, EventArgs e)
        {
            m_interface.Clear();

            DeleteAllData();
        }

        public void DeleteAllData()
        {
            // Delete the events (w/ their sounds)
            ArrayList eventList = m_eventTree.GetEvents();
            int eventOffset = 0;
            for (int evnt = 0, evntSize = eventList.Count; evnt < evntSize; ++evnt, ++eventOffset)
            {
                m_eventTree.RemoveEvent(((Event)eventList[evnt - eventOffset]).GetName());
            }

            // Delete the event tree events (w/ their sounds)
            TreeNodeCollection nodes = EventTree.Nodes;
            int nodeOffset = 0;
            for (int node = 0, nodeSize = nodes.Count; node < nodeSize; ++node, ++nodeOffset)
            {
                DeleteEventNode(nodes[node - nodeOffset]);
            }

            // Delete the variables
            ArrayList variableList = m_globalVarManager.GetGlobalVariables();
            int varOffset = 0;
            for (int var = 0, varSize = variableList.Count; var < varSize; ++var, ++varOffset)
            {
                m_globalVarManager.RemoveGlobalVariable(((GlobalVariable)variableList[var - varOffset]).m_name);
            }

            // Delete the sound bank sounds
            int totalRows = SoundBankGrid.Rows.Count;
            int rowOffset = 0;
            for (int soundRow = 0; soundRow < totalRows; ++soundRow, ++rowOffset)
            {
                SoundBankGrid.Rows[soundRow - rowOffset].Selected = true;
                m_soundLibrary.DeleteFile();
            }
        }

        private void DocumentationMenuItem_Click(object sender, EventArgs e)
        {
            // Notepad
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = "notepad";
            process.StartInfo.Arguments = "Tool README.txt";
            process.StartInfo.UseShellExecute = true;
            process.Start();
        }

        public string ParseFilename( string fullPath )
        {
              // decrement by one, to account for zero-based indexing
            int len = fullPath.Length - 1;

              // get the last character in the string
            char walkString = fullPath[len];

            string reverseName = null;

              // while the current index isn't equal to '\'
            while (walkString.CompareTo('\\') != 0 && len >= 0)
            {
                  // copy the character to what will be our reversed-string
                reverseName += walkString.ToString();

                  // decrement the index into the path
                --len;

                  // make sure not to index into crazy memory...
                if (len >= 0)
                {
                      // then get the next char
                    walkString = fullPath[len];
                }
            }

            string finalString = null;
            int finalIndex = reverseName.Length - 1;

            while (finalIndex >= 0)
            {
                  // get the reversed-strings last character, the file name's first character
                finalString += reverseName[finalIndex].ToString();
                finalIndex--;
            }

            return finalString;
        }
    }
}
